package com.example.a10928.weifriend.ui.personal_center

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter

import com.example.a10928.weifriend.App
import com.example.a10928.weifriend.R
import com.example.a10928.weifriend.adapter.BankInfoAdapter
import com.example.a10928.weifriend.adapter.CityAdapter
import com.example.a10928.weifriend.adapter.ProvinceAdapter
import com.example.a10928.weifriend.http.api.VariableName
import com.example.a10928.weifriend.ui.BaseActivity
import com.example.a10928.weifriend.util.RxUtil
import com.example.a10928.weifriend.util.StatusBarUtil
import com.githang.statusbar.StatusBarCompat
import kotlinx.android.synthetic.main.activity_select.*

import java.util.function.Consumer

class SelectActivity : BaseActivity() {


    override fun initData() {

    }

    override fun initView() {
        StatusBarCompat.setStatusBarColor(this, Color.WHITE,true)
        supportActionBar?.hide()
        recyc.addItemDecoration(DividerItemDecoration(this,DividerItemDecoration.VERTICAL))

        var manager=LinearLayoutManager(this)

        when (intent.getStringExtra(VariableName.TYPE)) {

            VariableName.PROVINCE ->{

                App.app().bpService.getProvinces(App.app().user.userId)
                        .compose(RxUtil.io2mainLoad(this))
                        .subscribe(
                                { r ->
                                    var adapter=ProvinceAdapter(R.layout.item_pickcountry,r.data.provinces)
                                    recyc.layoutManager=manager
                                    recyc.adapter=adapter

                                    adapter.setOnItemClickListener(object :BaseQuickAdapter.OnItemClickListener{
                                        override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

                                            var myIntent=Intent()
                                            myIntent.putExtra(VariableName.NAME,r.data.provinces.get(position).province)
                                            myIntent.putExtra(VariableName.ID,r.data.provinces.get(position).provinceid)
                                            setResult(Activity.RESULT_OK,myIntent)
                                            finish()
                                        }

                                    })

                                },
                                { e -> RxUtil.errorException(e) }
                        )

            }



            VariableName.CITY->{

                App.app().bpService.getCities(App.app().user.userId,intent.getStringExtra(VariableName.DATA))
                        .compose(RxUtil.io2mainLoad(this))
                        .subscribe(
                                { r ->
                                    var adapter=CityAdapter(R.layout.item_pickcountry,r.data.provinces)
                                    recyc.layoutManager=manager
                                    recyc.adapter=adapter

                                    adapter.setOnItemClickListener(object :BaseQuickAdapter.OnItemClickListener{
                                        override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

                                            var myIntent=Intent()
                                            myIntent.putExtra(VariableName.NAME,r.data.provinces.get(position).city)
                                            myIntent.putExtra(VariableName.ID,r.data.provinces.get(position).cityid)
                                            setResult(Activity.RESULT_OK,myIntent)
                                            finish()
                                        }

                                    })

                                },
                                { e -> RxUtil.errorException(e) }
                        )

            }

            VariableName.BANK->{

                App.app().bpService.getBankList(App.app().user.userId)
                        .compose(RxUtil.io2mainLoad(this))
                        .subscribe(
                                io.reactivex.functions.Consumer {
                                    var adapter=BankInfoAdapter(R.layout.item_pickcountry,it.data.records)
                                    recyc.layoutManager=manager
                                    recyc.adapter=adapter

                                    adapter.setOnItemClickListener(object :BaseQuickAdapter.OnItemClickListener{
                                        override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

                                            var myIntent=Intent()
                                            myIntent.putExtra(VariableName.NAME,it.data.records.get(position).bank_cn_name)
                                            myIntent.putExtra(VariableName.ID,it.data.records.get(position).bank_id)
                                            setResult(Activity.RESULT_OK,myIntent)
                                            finish()
                                        }

                                    })

                                },
                                io.reactivex.functions.Consumer {
                                    RxUtil.errorException(it)
                                }
                        )

            }



        }

    }

    override fun getContentView(): Int {
        return R.layout.activity_select
    }
}
